import Vue from 'vue'
import App from './App.vue'
import router from './router/index'
import store from './store/index'
// 引入ElementUI
import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css'
import './theme/element/index.css'
import 'font-awesome/css/font-awesome.min.css'

import { postRequest } from "./utils/api";
import { putRequest } from "./utils/api";
import { getRequest } from "./utils/api";
import { deleteRequest } from "./utils/api";
import { initMenu } from "./utils/menus";
import {downloadRequest} from "./utils/download";

Vue.config.productionTip = false
// 安装ElementUI
Vue.use(ElementUI)

//插件
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;

router.beforeEach((to, from, next) => {
     if (window.sessionStorage.getItem("tokenStr")) {
         initMenu(router, store);
         if (!window.sessionStorage.getItem("user")) {
             return getRequest('/admin/info').then(resp => {
                 if (resp) {
                     //存入用户信息
                     window.sessionStorage.setItem("user", JSON.stringify(resp));
                     store.commit('INIT_CURRENTADMIN',resp);
                     next();
                }
            });
        }
         next();
    } else {
         if (to.path == '/') {
             next();
        } else {
             next('/?redirect=' + to.path);
        }
    }
 })

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')